import { mapState, mapActions } from "vuex";
import invariant from "invariant";
export const getMapState = (state: any, nameSpaced) => {
    let res = {};
    if (typeof state === 'object') {
        let stateKey: Array<any> = [];
        if (state instanceof Array) {
            stateKey = state;
        }
        else {
            Object.keys(state).map((key) => {
                stateKey.push(key);
            })
        }
        stateKey.map((key) => {
            res[key] = state => {
                invariant(state[nameSpaced], `computed: ${key}, method: getMapState,error:store can not be null or undefined`);
                return state[nameSpaced][key]
            }
        })
    }
    return getState(res)
}
function getState(res) {
    let states = {
        ...mapState(res)
    };
    return states;
}
export const getMapPlainState = (state, nameSpaced) => {
    const res = {};
    Object.keys(state).forEach((key) => {
        res[key] = state => {
            let newState = state[nameSpaced][key];
            if (newState.isResolved && newState.data) {
                return newState.data.data;
            }
            return null;
        }
    })
    return getState(res);
}

export function getMapAction(type:Array<any>, nameSpaced) {
    if (type && type.constructor === Array) {
        let newAction = {};
        type.map((item) => {
            newAction[`${item}`] = item;
        })
        let action = {
            ...mapActions(newAction)
        };
        return action;
    }
    return {};
}